import UIKit

/* 类 */
// 1
class Vehicle {
    var use: String
    var speed: Int
    let producer: String
    var usedYear: Int
    
    var basicInfo: String {
        "use:\(use) speed:\(speed) producer:\(producer) usedYear:\(usedYear))"
    }
    
    func description() {
        print(basicInfo)
    }
    
    // 7
    init() {
        self.use = "Unknown"
        self.speed = 0
        self.producer = "Unknown"
        self.usedYear = 0
    }
    
    // 8
    init(use: String, speed: Int, producer: String, usedYear: Int) {
        self.use = use
        self.speed = speed
        self.producer = producer
        self.usedYear = usedYear
    }
}

let train = Vehicle(use: "train", speed: 300, producer: "producer1", usedYear: 10)

// 2
class Car: Vehicle {
    var owner: String
    var license: String
    
    func propertyTransfer(newOwner: String) {
        owner = newOwner
    }
    
    // 4.1
    override func description() {
        print(super.basicInfo + " owner:\(owner) license:\(license)")
    }
    
    // 5.1
    override var basicInfo: String {
        super.basicInfo + " owner:\(owner) license:\(license)"
    }
    
    // 6.1
    override var usedYear: Int {
        willSet {
            print("Car's usedYear will change")
        }
        didSet {
            print("Car's useYear changed")
            print("Old usedYear:\(oldValue) New usedYear:\(usedYear)")
        }
    }
    
    // 7
    override init() {
        self.owner = "Unknown"
        self.license = "Unknown"
        super.init()
    }
    
    // 8
    override init(use: String, speed: Int, producer: String, usedYear: Int) {
        self.owner = ""
        self.license = ""
        super.init(use: use, speed: speed, producer: producer, usedYear: usedYear)
    }
    
    convenience init(use: String, speed: Int, producer: String, usedYear: Int, owner: String, license: String) {
        self.init(use: use, speed: speed, producer: producer, usedYear: usedYear)
        self.owner = owner
        self.license = license
    }
    
    // 9.1
    convenience init(car: Car) {
        self.init(use: car.use, speed: car.speed, producer: car.producer, usedYear: car.usedYear,
                  owner: car.owner, license: car.license)
    }
}

let myCar = Car(use: "car", speed: 120, producer: "Volkswagen", usedYear: 3, owner: "owner1", license: "123456")
myCar.description()
print(myCar.owner)
myCar.propertyTransfer(newOwner: "owner2")
print(myCar.owner)

// 3
func annualInspection(vehicle: Vehicle) -> Bool{
    return  vehicle.usedYear > 5
}

print(annualInspection(vehicle: train))
print(annualInspection(vehicle: myCar))

// 4.2
myCar.description()

// 5.1
print(myCar.basicInfo)

// 6.1
myCar.usedYear += 1

// 9.2
var myNewCar = myCar
myNewCar.owner = "Jim"
let hisCar = Car(car: myNewCar)
